//
//  HJCodeGuideCollectionCell.m
//  Sneakers
//
//  Created by 胡光健 on 2017/10/12.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJCodeGuideCollectionCell.h"
#import "HJCodeCollectionCell.h"
@interface HJCodeGuideCollectionCell()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)UIButton * codeBtn;
@property(nonatomic,assign) NSIndexPath * cellRow;
@property(nonatomic,strong) NSMutableArray * shoesCodeArr;
@property(nonatomic,strong) NSNumber * check;
@end
@implementation HJCodeGuideCollectionCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.shoesCodeArr = [NSMutableArray array];
}
-(void)setModels:(NSArray *)models {
    _models = models;
    self.shoesCodeArr = [NSMutableArray arrayWithArray:models];
    [self.collectionView reloadData];
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self configCollection];
    }
    return self;
}
-(void)configCollection {
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    layout.itemSize = CGSizeMake(70, 70);
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.bounces = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"HJCodeCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"Cell"];
    [self addSubview:self.collectionView];
}
-(void)layoutSubviews {
    [super layoutSubviews];
    self.collectionView.frame = CGRectMake(0, 0, self.width, self.height);
}
//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.shoesCodeArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HJCodeCollectionCell * cell = (HJCodeCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    defaultSizeModel *model = self.shoesCodeArr[indexPath.row];
    cell.model = model;
    
    return cell;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    if (section==0) {
         return UIEdgeInsetsMake(0, 25, 0, 2.5);
    }
    return UIEdgeInsetsMake(0, 5, 0, 2.5);
}
//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0.001;
}

//点击选定
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//    HJCodeCollectionCell *cell = (HJCodeCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
    for (defaultSizeModel * model in self.shoesCodeArr) {
        if ([model.check isEqual:@1]) {
             model.check = @0;
        }
    }
    defaultSizeModel * model = self.shoesCodeArr[indexPath.row];
    model.check = @1;
//    cell.model = model;
    if (self.sizeBlock) {
        self.sizeBlock(model.size_num);
    }
    [collectionView reloadData];
}
//取消选定
//-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
//    HJCodeCollectionCell *cell = (HJCodeCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
//    if (self.sizeBlock) {
//        self.sizeBlock(@"");
//    }
//    defaultSizeModel * model = self.shoesCodeArr[indexPath.row];
//    model.check = @0;
//    cell.model = model;
//     NSLog(@"取消第%ld区，第%ld个",(long)indexPath.section,(long)indexPath.row);
//}


@end
